package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import org.jdesktop.application.Application;

import controller.MobilePlanSellAndBillSystem;
import customexceptions.ServiceAlreadyExistsException;
import customexceptions.UserNotExistsException;
import dto.LineBillDTO;
import dto.UserDTO;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class UserView extends javax.swing.JFrame {
	private JButton jButtonAccept;
	private JLabel jLabel1;
	private MobilePlanSellAndBillSystem system;
	private JTextField jTextFieldInvoice;
	private JLabel jLabelInvoice;
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				UserView inst = new UserView();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	public UserView(){
		
	}
	public UserView(MobilePlanSellAndBillSystem system) {
		super();
		initGUI();
		this.system = system;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			
			{
				jTextFieldInvoice = new JTextField();
				getContentPane().add(jTextFieldInvoice);
				jTextFieldInvoice.setBounds(111, 54, 333, 28);
			}
			{
				jLabelInvoice = new JLabel();
				getContentPane().add(jLabelInvoice);
				;
				jLabelInvoice.setBounds(12, 59, 62, 16);
				jLabelInvoice.setName("jLabelInvoice");
			}
				
			{
				jButtonAccept = new JButton();
				getContentPane().add(jButtonAccept);
				jButtonAccept.setText("View User");
				jButtonAccept.setBounds(211, 127, 84, 23);

				jButtonAccept.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent evt) 
					{
						try {
							
							String dni = jTextFieldInvoice.getText();
							if (dni.length()==0){
								ArrayList<UserDTO> users =  system.getUsers();
								GenericView view = new GenericView(users,true);
								view.setVisible(true);
							}else{
									UserDTO user =  system.getUser(jTextFieldInvoice.getText());
									GenericView view = new GenericView(user);
									view.setVisible(true);
							}
						} catch (UserNotExistsException e) {
							ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
							errorReportView.setVisible(true);
						}
					}
				});
				
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setBounds(12, 182, 260, 16);
				jLabel1.setName("jLabel1");
			}
			pack();
			this.setSize(506, 247);
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(getContentPane());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
